package controller;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet("/pickNameFindByTXT")
public class PickNameFindByTXTServlet extends HttpServlet {
    /**
     * 学生姓名提取功能（从html中正则匹配姓名输出到AllNames.txt文件中）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = "/Users/manchi520/Desktop/qianfeng/maven-io/src/main/resources/names.txt";
        search(path);
    }

    public void search(String url) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(url));
        BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/manchi520/Desktop/qianfeng/maven-io/src/main/resources/AllNames.txt"));
        String line = null;
        while ((line = br.readLine()) != null) {
            Pattern patt = Pattern.compile(".*>(.{2,3})</a>");
            Matcher matcher = patt.matcher(line);
            if (matcher.find()){
                String s = matcher.group(1);
//                System.out.println(s);
                bw.write(s + "\n");
            }
        }
        bw.close();
        br.close();
    }
}
